//
//  NetWorking.m
//  Newproject3
//
//  Created by rimi on 16/9/21.
//  Copyright © 2016年 Liang. All rights reserved.
//

#import "NetWorking.h"
#import <AFNetworking/AFNetworking.h>
static NetWorking *manger = nil;
@implementation NetWorking
+(NetWorking *)shareNetWork{
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manger = [[NetWorking alloc]init];
    });
    return manger;
}

-(void)startGetRequestWithUrl:(NSString *)URL paramters:(NSDictionary *)paramter success:(NetBlock)successBlock failed:(NetBlock)failed{
     AFHTTPSessionManager *manage = [AFHTTPSessionManager manager];
    manage.requestSerializer.timeoutInterval = 20;
    manage.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json",@"text/html",@"text/javascript", nil];
    manage.responseSerializer.acceptableContentTypes = [manage.responseSerializer.acceptableContentTypes setByAddingObjectsFromArray:@[@"text/plain"]];
    [manage.requestSerializer setValue:@"ee5fcdbb21624a5cd90511247d13ca96"forHTTPHeaderField:@"apikey"];
    [manage GET:URL parameters:paramter progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        successBlock(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failed(error);
    }];
}

-(void)startPostRequestWithUrl:(NSString *)URL paramters:(NSDictionary *)paramter success:(NetBlock)successBlock failed:(NetBlock)failed{
    AFHTTPSessionManager *manage = [AFHTTPSessionManager manager];
   
    manage.requestSerializer.timeoutInterval = 20;
    manage.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json",@"text/html",@"text/javascript", nil];
    manage.responseSerializer.acceptableContentTypes = [manage.responseSerializer.acceptableContentTypes setByAddingObjectsFromArray:@[@"text/html"]];
    [manage POST:URL parameters:paramter constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        successBlock(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failed(error);
    }];
}

@end
